package com.xiaoyu.client.ui.fragment.main;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.xiaoyu.client.R;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.PreferencesUtil;


import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MainActivity extends BaseActivity implements View.OnClickListener, TabHost.OnTabChangeListener {
    protected static FragmentTabHost mTabHost;

    protected static final String TAB_TAG_FOR_HOME = "0";
    protected static final String TAB_TAG_FOR_SHOP = "1";
    protected static final String TAB_TAG_FOR_BILLION = "2";
    protected static final String TAB_TAG_FOR_HELP = "3";
    protected static final String TAB_TAG_FOR_MINE = "4";

    @BindView(R.id.main_tab_home)
    LinearLayout mTabHome;//首页
    @BindView(R.id.main_tab_shop)
    LinearLayout mTabShop;//商城
    @BindView(R.id.main_tab_help)
    LinearLayout mTabHelp;//求助
    @BindView(R.id.main_tab_mine)
    LinearLayout mTabMine;//我的
    @BindView(R.id.main_tab_billion_circle)
    LinearLayout mTabCircle;

    @BindView(R.id.main_tab_home_img)
    ImageView mHomeImg;
    @BindView(R.id.main_tab_shop_img)
    ImageView mShopImg;
    @BindView(R.id.main_tab_help_img)
    ImageView mHelpImg;
    @BindView(R.id.main_tab_mine_img)
    ImageView mMineImg;

    @BindView(R.id.main_tab_home_text)
    TextView mHomeTxt;
    @BindView(R.id.main_tab_shop_text)
    TextView mShopTxt;
    @BindView(R.id.main_tab_help_text)
    TextView mHelpTxt;
    @BindView(R.id.main_tab_mine_text)
    TextView mMineTxt;

    @BindView(R.id.main_tab_billion_circle_img)
    ImageView mCircleImg;
    @BindView(R.id.main_tab_billion_circle_txt)
    TextView mCircleTxt;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPerssion();
        ButterKnife.bind(this);
        initView();
        setTagAndAlias();
    }


    private void initView() {
        mTabHome.setOnClickListener(this);
        mTabShop.setOnClickListener(this);
        mTabHelp.setOnClickListener(this);
        mTabMine.setOnClickListener(this);
        mTabCircle.setOnClickListener(this);

        initTabHost();

    }

    private void initTabHost() {
        mTabHost = this.findViewById(R.id.main_tabhost);
        mTabHost.setup(this, this.getSupportFragmentManager(), R.id.tab_content_layout);
        mTabHost.getTabWidget().setVisibility(View.GONE);
        mTabHost.getTabWidget().setDividerDrawable(null);
        mTabHost.setOnTabChangedListener(this);

        setOccupyViewLinearTransparencyLogin();
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_HOME).setIndicator("home"),
                HomeFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_SHOP).setIndicator("shop"),
                MallFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_BILLION).setIndicator("billion"),
                BillionCircleFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_HELP).setIndicator("help"),
                HelpFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_MINE).setIndicator("mine"),
                MyFragment.class, null);

    }

    @Override
    public void onClick(View v) {
        if (v == mTabHome) {
            setOccupyViewLinearTransparencyLogin();
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_HOME);
        } else if (v == mTabShop) {
            setOccupyViewLinearGradientLogin();
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_SHOP);
        } else if (v == mTabCircle) {
            setOccupyViewLinearGradientLogin();
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_BILLION);
        } else if (v == mTabHelp) {
            setOccupyViewLinearGradientLogin();
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_HELP);
        } else if (v == mTabMine) {
            setOccupyViewLinearGradient();
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_MINE);
        }
    }

    @Override
    public void onTabChanged(String tabId) {
        if (TAB_TAG_FOR_HOME.equals(tabId)) {
            mHomeImg.setImageResource(R.drawable.tab_home_blue);
            mShopImg.setImageResource(R.drawable.tab_shopping_gray);
            mHelpImg.setImageResource(R.drawable.tab_help_gray);
            mMineImg.setImageResource(R.drawable.tab_mine_gray);
            mCircleImg.clearAnimation();

            mHomeTxt.setTextColor(Color.parseColor("#1772E5"));
            mShopTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mHelpTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMineTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mCircleTxt.setTextColor(Color.parseColor("#A2AAB3"));

        } else if (TAB_TAG_FOR_SHOP.equals(tabId)) {
            mHomeImg.setImageResource(R.drawable.tab_home_gray);
            mShopImg.setImageResource(R.drawable.tab_shopping_blue);
            mHelpImg.setImageResource(R.drawable.tab_help_gray);
            mMineImg.setImageResource(R.drawable.tab_mine_gray);
            mCircleImg.clearAnimation();
            mHomeTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mShopTxt.setTextColor(Color.parseColor("#1772E5"));
            mHelpTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMineTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mCircleTxt.setTextColor(Color.parseColor("#A2AAB3"));
        } else if (TAB_TAG_FOR_BILLION.equals(tabId)) {

            mHomeImg.setImageResource(R.drawable.tab_home_gray);
            mShopImg.setImageResource(R.drawable.tab_shopping_gray);
            mHelpImg.setImageResource(R.drawable.tab_help_gray);
            mMineImg.setImageResource(R.drawable.tab_mine_gray);

            Animation animation = AnimationUtils.loadAnimation(this, R.anim.img_animation);
            LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
            animation.setInterpolator(lin);
            mCircleImg.startAnimation(animation);

            mHomeTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mShopTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mHelpTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMineTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mCircleTxt.setTextColor(Color.parseColor("#1772E5"));

        } else if (TAB_TAG_FOR_HELP.equals(tabId)) {
            mHomeImg.setImageResource(R.drawable.tab_home_gray);
            mShopImg.setImageResource(R.drawable.tab_shopping_gray);
            mHelpImg.setImageResource(R.drawable.tab_help_blue);
            mMineImg.setImageResource(R.drawable.tab_mine_gray);
            mCircleImg.clearAnimation();
            mHomeTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mShopTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mHelpTxt.setTextColor(Color.parseColor("#1772E5"));
            mMineTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mCircleTxt.setTextColor(Color.parseColor("#A2AAB3"));
        } else if (TAB_TAG_FOR_MINE.equals(tabId)) {
            mHomeImg.setImageResource(R.drawable.tab_home_gray);
            mShopImg.setImageResource(R.drawable.tab_shopping_gray);
            mHelpImg.setImageResource(R.drawable.tab_help_gray);
            mMineImg.setImageResource(R.drawable.tab_mine_blue);
            mCircleImg.clearAnimation();
            mHomeTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mShopTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mHelpTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMineTxt.setTextColor(Color.parseColor("#1772E5"));
            mCircleTxt.setTextColor(Color.parseColor("#A2AAB3"));
        }
    }


    /**
     * Android 6.0权限适配
     */
    private void requestPerssion() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    /**
     * 设置标签与别名
     */
    private void setTagAndAlias() {
        /**
         *这里设置了别名，在这里获取的用户登录的信息
         *并且此时已经获取了用户的userId,然后就可以用用户的userId来设置别名了
         **/
        //false状态为未设置标签与别名成功
        // if (UserUtils.getTagAlias(getHoldingActivity()) == false) {
        Set<String> tags = new HashSet<String>();
        // 这里可以设置你要推送的人，一般是用户uid 不为空在设置进去 可同时添加多个
        if (!TextUtils.isEmpty(PreferencesUtil.getString(PreferencesUtil.UID, ""))) {
            tags.add(PreferencesUtil.getString(PreferencesUtil.UID, ""));
        }
        Log.i("jx", "setTagAndAlias:uid==== " + PreferencesUtil.getString(PreferencesUtil.UID, ""));
        // 设置tag
        // 上下文、别名【Sting行】、标签【Set型】、回调
        JPushInterface.setAliasAndTags(MainActivity.this, PreferencesUtil.getString(PreferencesUtil.UID, ""), tags, mAliasCallback);
    }


    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    Log.i("jx", "gotResult: 别名：===" + alias);
                    break;
                case 6002:
                    //极低的可能设置失败 我设置过几百回 出现3次失败 不放心的话可以失败后继续调用上面那个方面 重连3次即可 记得return 不要进入死循环了...
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败，60秒后重试";
                    break;
                default:
                    logs = "极光推送设置失败，Failed with errorCode = " + code;
                    break;
            }
        }
    };
}


